Skip to content

Add convert_segmentation_map_to_binary_masks_sorted function for hand… - #40446

Closed
Ahmed-G-ElTaher wants to merge 3 commits into
huggingface:mainfrom
Ahmed-G-ElTaher:fix-mask2former-overlapping-annotations
Closed

Ahmed-G-ElTaher wants to merge 3 commits into
huggingface:mainfrom
Ahmed-G-ElTaher:fix-mask2former-overlapping-annotations

Conversation

@Ahmed-G-ElTaher

@Ahmed-G-ElTaher Ahmed-G-ElTaher commented Aug 26, 2025 •

Copy link
Copy Markdown

Pull Request: Add Support for Handling Overlapping Annotations in Mask2Former

Problem

The current image processing pipeline for Mask2Former doesn't handle overlapping annotations correctly. When annotations overlap, the processing order is arbitrary, causing larger objects to sometimes be overwritten by smaller ones. This can lead to information loss and incorrect segmentation masks.

Solution

This PR introduces a new function, convert_segmentation_map_to_binary_masks_sorted, that addresses this issue by processing object instances in descending order of their area. This ensures that smaller objects consistently appear on top of larger objects, preserving all annotation information in overlapping regions.

The implementation draws inspiration from the approach used in the coco2masks function, which sorts annotations by area before processing them. This enhancement is particularly beneficial for datasets with significant object overlaps.

Changes

A new function convert_segmentation_map_to_binary_masks_sorted has been added with the following features:

  • Accepts an optional sort_by_area parameter (default: True).
  • Calculates the area of each instance when sorting is enabled.
  • Processes instances in descending order of their area.

This new function can serve as a direct replacement for the existing conversion function, providing improved handling of overlapping regions.

Testing

  • Tested with sample COCO annotations containing overlapping objects.
  • Verified that all objects are correctly represented in the output masks.
  • Confirmed compatibility with existing Mask2Former models.

Performance Impact

While the area calculation introduces a minor computational overhead, its impact is negligible within the overall processing pipeline. This step occurs during preprocessing rather than during model inference.

Ahmed-G-ElTaher and others added 3 commits August 26, 2025 05:45
…ling overlapping annotations

This commit adds a new function that extends the base mask conversion functionality with area-based sorting capability. Key improvements:

- Sorts segmentation masks by area (largest first) to handle overlapping annotations
- Maintains compatibility with existing convert_segmentation_map_to_binary_masks API
- Includes comprehensive tests covering basic functionality, edge cases, instance mapping, and area ordering
- Provides detailed documentation with examples demonstrating usage for overlapping object scenarios

The area-based sorting is particularly useful for Mask2Former models when processing images with overlapping object annotations, ensuring consistent mask ordering for training and inference.
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: mask2former

@Rocketknight1

Copy link
Copy Markdown
Member

cc @qubvel @NielsRogge

@ArthurZucker

Copy link
Copy Markdown
Collaborator

Thanks for the PR, and sorry it didn't make it in! This branch is based on v4, and v5 is very different, so rather than rebasing we'd love to invite you to open a new PR against main using our latest tools and good practices:

Closing this one in the meantime, thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants